﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace COC_helper
{
    public partial class PeoInfo : Form
    {
        private int Count;
        public PeoInfo()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void PeoInfo_Shown(object sender, EventArgs e)
        {
            if (Global_Value.zmb_Setting.peoEdites == null)
            {
                MessageBox.Show("人物库丢失!!");
                return;
            }
            ListBox_Group.Items.Clear();
            ListBox_Name.Items.Clear();
            try
            {
                foreach (var item in Global_Value.zmb_Setting.peoEdites)
                {
                    if (!ListBox_Group.Items.Contains(item.Group))
                        ListBox_Group.Items.Add(item.Group);
                    Count++;
                    ListBox_Name.Items.Add(item.Name);
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString()); return;

            }

            // MessageBox.Show("找到了:" + Count.ToString());




        }

        private void Button_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (ListBox_Name.SelectedItem == null)
                    return;
            }
            catch (Exception)
            {
                ListBox_Name.SelectedIndex = 0;
                
            }
            if (TextBox_Name.Text == "")
            {
                MessageBox.Show("人不能没有名字");
                return;
            }
            if (TextBox_Group.Text == "")
            {
                MessageBox.Show("人不能没有派系归属");
                return;
            }
            try
            {
   
                Global_Value.zmb_Setting.peoEdites.Remove(Global_Value.zmb_Setting.peoEdites.Find(one => one.Name == ListBox_Name.SelectedItem.ToString()));
                
                ListBox_Name.Items.Remove(ListBox_Name.SelectedItem);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString()); return;
               
            }
            

            PeoEdite peo = new PeoEdite()
            {
                Name = TextBox_Name.Text,
                Group = TextBox_Group.Text,
                State = TextBox_State.Text,
                Type = TextBox_Type.Text,
                Info = TextBox_Info.Text,
                Thing = TextBox_Thing.Text,
                Tag = TextBox_Tag.Text,
                Place = TextBox_Place.Text,
                Tale = TextBox_Tale.Text,

            };
            if (!ListBox_Group.Items.Contains(peo.Group))
                ListBox_Group.Items.Add(peo.Group);
            Global_Value.zmb_Setting.peoEdites.Add(peo);
            ListBox_Name.Items.Add(peo.Name);
            Button_Add.Visible = true;
            Button_New.Visible = true;
            Button_Del.Visible = true;

            Button_New.Tag = "edit";
            ListBox_Name.Enabled = true;

        }

        private void Button_New_Click(object sender, EventArgs e)
        {
           
                Button_Add.Visible = false;
                Button_New.Visible = false;
            Button_Del.Visible = false;

            Button_New.Tag = "wait";
            ListBox_Name.Items.Add("新人物");
            ListBox_Name.SelectedIndex = ListBox_Name.Items.IndexOf("新人物");
            ListBox_Name.Enabled = false;
            TextBox_Group.Text = TextBox_Name.Text = TextBox_Tag.Text = TextBox_Tale.Text = "";
            TextBox_Info.Text = TextBox_Key.Text = TextBox_Place.Text = TextBox_Place.Text = "";
            TextBox_State.Text = TextBox_Type.Text = "";
        }

        private void Button_Add_Click(object sender, EventArgs e)
        {

            Button_Add.Visible = false;
            Button_New.Visible = false;
            Button_Del.Visible = false;
            Button_New.Tag = "wait";
            ListBox_Name.Items.Add("新人物");
            ListBox_Name.SelectedIndex = ListBox_Name.Items.IndexOf("新人物"); 
            ListBox_Name.Enabled = false;
            TextBox_Name.Text = "";
        }

        private void Button_Del_Click(object sender, EventArgs e)
        {
            if (ListBox_Name.SelectedItems == null)
            {
                return;
            }
            try
            {
                Global_Value.zmb_Setting.peoEdites.Remove(Global_Value.zmb_Setting.peoEdites.Find(one => one.Name == ListBox_Name.SelectedItem.ToString()));

                ListBox_Name.Items.Remove(ListBox_Name.SelectedItem);
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString()); return;


            }
        }

        private void ListBox_Name_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListBox_Name.SelectedItem == null)
                return;
            //ListBox_Group.Items.Clear();
            try
            {
                foreach (PeoEdite item in Global_Value.zmb_Setting.peoEdites)
                {
                    if (item.Name == ListBox_Name.SelectedItem.ToString())
                    {
                        TextBox_Name.Text = item.Name;
                        TextBox_Group.Text = item.Group;
                        TextBox_State.Text = item.State;
                        TextBox_Type.Text = item.Type;
                        TextBox_Info.Text = item.Info;
                        TextBox_Thing.Text = item.Thing;
                        TextBox_Tag.Text = item.Tag;
                        TextBox_Place.Text = item.Place;
                        TextBox_Tale.Text = item.Tale;

                        return;
                    }
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString()); return;
            }

                Console.WriteLine("人物面板: 名字列表指向了个不存在的名字:" + ListBox_Name.SelectedItem.ToString());
            

        }

        private void ListBox_Group_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ListBox_Group.SelectedItem == null) return;
            ListBox_Name.Items.Clear();
            int Count = 0;
            foreach (var item in Global_Value.zmb_Setting.peoEdites)
            {
                if (item.Group == ListBox_Group.SelectedItem.ToString())
                {
                    ListBox_Name.Items.Add(item.Name);
                    Count++;
                }
            }
            label2.Text = "筛选 " + ListBox_Group.SelectedItem.ToString() + " 共 "  + Count + "个";

        }

        private void label2_Click(object sender, EventArgs e)
        {
            ListBox_Name.Items.Clear();
            int Count = 0;
            foreach (var item in Global_Value.zmb_Setting.peoEdites)
            {
                
                 ListBox_Name.Items.Add(item.Name);
                 Count++;
                
            }
            label2.Text = "清除筛选 " +  " 共 " + Count + "个";
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="mod">是否为基于现有结果查找</param>
        private void Finding(bool mod)
        { 
            if (TextBox_Key.Text == "")
            {
                MessageBox.Show("待搜索的关键词不能为空值");
                return;
            }
            int Count = 0;
            progressBar1.Maximum = Global_Value.zmb_Setting.peoEdites.Count;
            progressBar1.Value = 0;
            List<PeoEdite> temp = new List<PeoEdite>();
            foreach (var item in Global_Value.zmb_Setting.peoEdites)
            {
                if(!mod || ListBox_Name.Items.Contains( item.Name) )
                    temp.Add(item);

            }
            ListBox_Name.Items.Clear();
            foreach (var item in temp)
            {


                progressBar1.Value++;
                if (checkBox1.Checked && item.Name.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox2.Checked && item.Group.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox3.Checked && item.Tag.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox4.Checked && item.Info.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox5.Checked && item.Type.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox6.Checked && item.State.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox7.Checked && item.Thing.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox8.Checked && item.Tale.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else if (checkBox9.Checked && item.Place.IndexOf(TextBox_Key.Text) != -1)
                    ListBox_Name.Items.Add((string)item.Name);
                else
                    continue;
                
                Count++;

            }
            label2.Text = "查找到:" + Count;
            progressBar1.Value = 0;

        }

        /// <summary>
        /// 重新开始搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Research_Click(object sender, EventArgs e)
        {
            Finding(false);
        }

        private void Button_Search_Click(object sender, EventArgs e)
        {
            Finding(true);
        }
    }
}

